package com.qire.manhua.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.common.basic.DateUtil;
import com.qire.common.constant.CacheStorageKey;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.CacheStorageUtil;
import com.qire.manhua.R;
import com.qire.manhua.databinding.DialogTeensEnablePromptBinding;

import androidx.annotation.NonNull;

/**
 * 青少年模式启用提示弹窗
 */
public class TeensEnablePromptDialog extends BaseDialog {

    private static int callCount = 0;

    /**
     * 如果需要则构建并显示窗口
     * @param context
     */
    public static void buildAndShowIfNeeded(Context context) {
        callCount++;
        if(callCount <= 1 || !UserEntity.self.isLogin() || TeensModeManage.teensModeEnable()) {
            return;
        }

        long lastShowTime = CacheStorageUtil.queryLong(CacheStorageKey.TEENS_ENABLE_PROMPT);
        boolean notTimeOut = DateUtil.yMdHms.assertToTodayValid(lastShowTime, 1);
        if(!notTimeOut) {
            new TeensEnablePromptDialog(context).postShow();
            CacheStorageUtil.save(CacheStorageKey.TEENS_ENABLE_PROMPT, System.currentTimeMillis());
        }
    }

    private DialogTeensEnablePromptBinding viewDataBinder;

    private TeensEnablePromptDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog_Translucent);
        viewDataBinder = DialogTeensEnablePromptBinding.inflate(LayoutInflater.from(context));
        viewDataBinder.setTeensEnablePromptDialog(this);

        setContentView(viewDataBinder.getRoot());
        setCancelable(false);
    }

    public void enableBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.TeensControlPanelPage).navigation();
        dismiss();
    }

    public void closeBtnOnClick(View view) {
        dismiss();
    }

}
